package org.light.limitedverb;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.Method;
import org.light.domain.Signature;
import org.light.domain.Statement;
import org.light.domain.Type;
import org.light.exception.ValidateException;
import org.light.utils.StringUtil;
import org.light.utils.TableStringUtil;
import org.light.utils.WriteableUtil;

public class CountActiveRecords extends NoControllerVerb {

	@Override
	public Method generateDaoImplMethod() throws Exception {
		if (this.denied)
			return null;
		else {
			Method method = new Method();
			method.setStandardName("CountActive" + domain.getCapFirstDomainName() + "Records");
			method.setNoContainer(false);
			method.addSignature(new Signature(1, "&self",""));
			method.setReturnType(new Type("Result<CountNum, sqlx::Error>"));
			
			List<Writeable> sList = new ArrayList<Writeable>();

			sList.add(new Statement(500L,2,"let result = sqlx::query_as("));
			if ("oracle".equalsIgnoreCase(this.domain.getDbType())) {
				sList.add(new Statement(1000L,2,"r#\"select count(*) as countNum from " + domain.getDbPrefix() + TableStringUtil.domainNametoTableName(domain) + " where " + 
						this.domain.getActive().getFeildNameAsTableColumn() + " = " + 
						this.domain.getDomainActiveInteger() + "\"#"));
			}else {
				sList.add(new Statement(1000L,2,"r#\"select count(*) as countNum from " + domain.getDbPrefix() + TableStringUtil.domainNametoTableName(domain) + " where " + 
					this.domain.getActive().getFeildNameAsTableColumn() + " = " + 
					this.domain.getDomainActiveStr() + "\"#"));
			}
			sList.add(new Statement(4000L,2,")"));
			sList.add(new Statement(5000L,2,".fetch_one(&*self.pool)"));
			sList.add(new Statement(6000L,2,".await;"));
			sList.add(new Statement(10000L,2,"self.pool.close();"));
			sList.add(new Statement(11000L,2,"return result;"));

			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;
		}
	}

	@Override
	public String generateDaoImplMethodString() throws Exception {
		if (this.denied)
			return null;
		else {
			Method m = this.generateDaoImplMethod();
			String s = m.generateMethodString();
			return s;
		}
	}

	@Override
	public String generateDaoImplMethodStringWithSerial() throws Exception {
		if (this.denied)
			return null;
		else {
			Method m = this.generateDaoImplMethod();
			m.setContent(m.generateMethodContentStringWithSerial());
			m.setMethodStatementList(null);

			return m.generateMethodString();
		}
	}

	@Override
	public Method generateDaoMethodDefinition() throws Exception {
		if (this.denied)
			return null;
		else {
			Method method = new Method();
			method.setStandardName("CountActive" + this.domain.getCapFirstDomainName() + "Records");
			method.addSignature(new Signature(1, "db","*sql.DB"));
			method.setReturnType(new Type("int64"));
			return method;
		}
	}

	@Override
	public String generateDaoMethodDefinitionString() throws Exception {
		if (this.denied)
			return null;
		else {
			return generateDaoMethodDefinition().generateMethodDefinition();
		}
	}

	@Override
	public Method generateServiceMethodDefinition() throws Exception {
		if (this.denied)
			return null;
		else {
			Method method = new Method();
			method.setStandardName("CountActive" + this.domain.getCapFirstDomainName() + "Records");
			method.setReturnType(new Type("int64"));

			return method;
		}
	}

	@Override
	public String generateServiceMethodDefinitionString() throws Exception {
		if (this.denied)
			return null;
		else {
			return generateServiceMethodDefinition().generateMethodDefinition();
		}
	}

	@Override
	public Method generateServiceImplMethod() throws Exception {
		if (this.denied)
			return null;
		else {
			Method method = new Method();
			method.setStandardName("CountActive" + this.domain.getCapFirstDomainName() + "Records");
			method.setReturnType(new Type("Result<CountNum, sqlx::Error>"));
			
			Method daomethod = this.generateDaoMethodDefinition();
			List<Writeable> sList = new ArrayList<Writeable>();
			sList.add(new Statement(1000L,1,"let app_state = init_db();"));
			sList.add(new Statement(2000L,1,"app_state.await.context."+StringUtil.getSnakeName(this.domain.getPlural())+"."+StringUtil.getSnakeName(this.getVerbName())+"().await"));

			method.setMethodStatementList(WriteableUtil.merge(sList));

			return method;
		}
	}

	@Override
	public String generateServiceImplMethodString() throws Exception {
		if (this.denied)
			return null;
		else {
			return generateServiceImplMethod().generateMethodString();
		}
	}

	@Override
	public String generateServiceImplMethodStringWithSerial() throws Exception {
		if (this.denied)
			return null;
		else {
			Method m = this.generateServiceImplMethod();
			m.setContent(m.generateMethodContentStringWithSerial());
			m.setMethodStatementList(null);
			return m.generateMethodString();
		}
	}

	public CountActiveRecords(Domain domain) throws ValidateException {
		super();
		this.domain = domain;
		this.denied = domain.isVerbDenied("ToggleOne");
		this.verbName = "countActive" + this.domain.getCapFirstDomainName() + "Records";
	}

	public CountActiveRecords() {
		super();
	}

}
